package io.zhifei.modules.sys.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.R;
import io.zhifei.common.utils.ToolUtils;
import io.zhifei.modules.sys.system.dao.MailClassDao;
import io.zhifei.modules.sys.system.entity.MailClassEntity;
import io.zhifei.modules.sys.system.form.MailClassParam;
import io.zhifei.modules.sys.system.form.MailClassResult;
import io.zhifei.modules.sys.system.service.MailClassService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 班级邮件模板 服务实现类
 * </p>
 *
 * @author duke
 * @since 2021-06-07
 */
@Service
public class MailClassServiceImpl extends ServiceImpl<MailClassDao, MailClassEntity> implements MailClassService {

    /**
     * 发件人邮箱
     */
    @Value("${spring.mail.username}")
    private String from;

    @Resource
    private JavaMailSender mailSender;

    @Override
    public void add(MailClassParam param) {
        MailClassEntity entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MailClassParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(MailClassParam param) {
        MailClassEntity oldEntity = getOldEntity(param);
        MailClassEntity newEntity = getEntity(param);
        ToolUtils.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MailClassResult findBySpec(MailClassParam param) {
        return this.baseMapper.getDetail(param);
    }


    @Override
    public PageUtils findPageBySpec(Page page,MailClassParam param) {
        IPage pageList = this.baseMapper.getPageList(page,param);
        return new PageUtils(pageList);
    }

    @Override
    public List<MailClassResult> getList(MailClassParam param) {
        return this.baseMapper.getList(param);
    }

    @Override
    public R sendEmail(MailClassParam param) {
        if (StrUtil.isEmpty(param.getEmail())){
            return R.error("请输入要发送的地址！");
        }
        try {
            //使用JavaMail的MimeMessage，支付更加复杂的邮件格式和内容
            MimeMessage message = mailSender.createMimeMessage();
            //创建MimeMessageHelper对象，处理MimeMessage的辅助类
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //使用辅助类MimeMessage设定参数
            helper.setFrom(from);
            helper.setTo(param.getEmail());
            helper.setSubject(param.getTitle());
            helper.setText(param.getContent(),true);
            mailSender.send(message);
            return R.ok();
        }catch (MessagingException e){
            return R.error("发送邮件失败，请联系管理员");
        }
    }

    private Serializable getKey(MailClassParam param) {
        return param.getId();
    }

    private MailClassEntity getOldEntity(MailClassParam param) {
        return this.getById(getKey(param));
    }

    private MailClassEntity getEntity(MailClassParam param) {
        MailClassEntity entity = new MailClassEntity();
        ToolUtils.copyProperties(param, entity);
        return entity;
    }

}
